package com.xiaozhen.blog.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xiaozhen.blog.entity.Blog;
import com.xiaozhen.blog.entity.Tag;
import com.xiaozhen.blog.mapper.BlogMapper;
import com.xiaozhen.blog.mapper.TagMapper;
import com.xiaozhen.blog.service.TagService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @create 2023-04-21 20:17
 */
@Service
public class TagServiceImpl extends ServiceImpl<TagMapper, Tag> implements TagService {

    @Autowired
    private TagMapper tagMapper;

    @Autowired
    private BlogMapper blogMapper;

    /**
     * 根据该标签的博客数量，获取前 6 个标签
     *
     * @return
     */
    public List<Tag> tagListbByCount() {
        // 取数量最多的前 6 个标签
        LambdaQueryWrapper<Tag> tagWrapper = new LambdaQueryWrapper<>();
        tagWrapper.orderByDesc(Tag::getCount);
        tagWrapper.last("limit 0,6");
        List<Tag> tagList = tagMapper.selectList(tagWrapper);
        return tagList;
    }

    /**
     * 查询所有的标签
     * @return
     */
    @Override
    public List<Tag> tagList() {
        return tagMapper.selectList(null);
    }

    /**
     * 根据标签 id 查询博客列表
     *
     * @param pageParam
     * @return
     */
    @Override
    public Object blogPage(Page<Blog> pageParam, Long tagId) {
        LambdaQueryWrapper<Blog> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(Blog::getTypeId,tagId);
        wrapper.orderByDesc(Blog::getUpdateTime);
        return blogMapper.selectPage(pageParam,wrapper);
    }

    /**
     * 分页查询所有标签列表
     * @param page
     * @return
     */
    @Override
    public Page<Tag> tagPage(Long page) {
        // 如果没有传入参数，那么我们把page定义为第 1 页
        if (page == null) {
            page = 1L;
        }
        // 创建对象，传递分页相关参数，每页记录数为 6
        Page<Tag> pageParam = new Page<>(page, 6);
        Page<Tag> tagPage = tagMapper.selectPage(pageParam,null);
        return tagPage;
    }
}
